﻿using System.Diagnostics;
using Testing.Helpers.Assertion;

namespace Testing.Helpers.Processes
{
    /// <summary>
    /// Helper for the process.
    /// </summary>
    public static class ProcessHelper
    {
        /// <summary>
        /// Run the process and wait for its completion during timeout.
        /// </summary>
        /// <param name="fileName">Full path to the application.</param>
        /// <param name="timeout">Timeout of application execution.</param>
        public static void RunProcess(string fileName, int timeout)
        {
            var p = new Process { StartInfo = new ProcessStartInfo(fileName) };

            p.Start();
            p.WaitForExit(timeout);
            if (!p.HasExited)
            {
                throw new TestExecutionException("Process  + «" + fileName +
                    "» is not completed during a timeout " + timeout + "mc.");
            }
            if (p.ExitCode != 0)
            {
                throw new TestExecutionException("Process + «" + fileName + "» failed.");
            }
        }

        /// <summary>
        /// Run the process and wait for its completion during timeout.
        /// </summary>
        /// <param name="fileName">Full path to the application.</param>
        /// <param name="arguments">Arguments.</param>
        /// <param name="timeout">Timeout of application execution.</param>
        public static void RunProcess(string fileName, string arguments, int timeout)
        {
            var p = Process.Start(fileName, arguments);
            p.WaitForExit(timeout);
            if (!p.HasExited)
            {
                throw new TestExecutionException("Process + «" + fileName + 
                    " " + arguments + "» is not completed during a timeout " + timeout + "mc.");
            }
            if (p.ExitCode != 0)
            {
                throw new TestExecutionException("Process + «" + fileName +
                    " " + arguments + "» failed.");
            }
        }
    }
}
